<?php  
	namespace Home\Controller;
	use Think\Controller;
	//事假管理
	//@author  程传民
	class LeaveController extends AllowController{

		//我的事假申请
		public function index(){
			$leave = D("leave");
			$users = D("users");
			$arr1 = array("pid"=>$_SESSION['userid']);
			$info = $users->field("id,name,can_login")->select();

			$tot = $leave->where($arr1)->count();

			// 分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	
			$list = $leave->where($arr1)->order("created desc")->limit($page->firstRow,$page->listRows)->select();

			foreach($list as $key => $value){
				// 请假人姓名
				foreach($info as $k => $v){
					if($list[$key]['pid'] == $v['id']){
						$list[$key]['pid'] = $v['name'];
					}
				}
				// 假条状态
				
				if($list[$key]['status']==1){
					$list[$key]['status'] = "待审核";
					$list[$key]['color'] = "#009688";
				}else if($list[$key]['status']>1 && $list[$key]['status']<4){
					$list[$key]['status'] = "审核中";
					$list[$key]['color'] = "#5FB878";
				}else if($list[$key]['status']==0){
					$list[$key]['status'] = "已被拒绝";
					$list[$key]['color'] = "#FF5722";
				}else if($list[$key]['status']==4){
					$list[$key]['status'] = "已通过";
					$list[$key]['color'] = "#01AAED";
				}else if($list[$key]['status']==5){
					$list[$key]['status'] = "假期结束";
					$list[$key]['color'] = "#2F4056";
				}
				// 请假天数
				$list[$key]['day'] = ceil(($list[$key]['end_time']-$list[$key]['start_time'])/(24*60*60));
				//开始时间
				$list[$key]['start_time'] = date("Y-m-d",$list[$key]['start_time']);
				//结束时间
				$list[$key]['end_time'] = date("Y-m-d",$list[$key]['end_time']);
			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);

			$this->assign("pageinfo",$page->show());
			$this->assign("list",$list);
			$this->display("Leave/index");
		}


		public function add(){
			$users = D("users");
			$arr = array("id"=>$_SESSION['userid']);
			$list = $users->where($arr)->field("id,name,class")->find();
			$this->assign("list",$list);
			$this->display("Leave/add");

		}
		public function insert(){
			$leave = D("leave");

			//得到的时间戳是毫秒数
			$data['class'] = I("post.classid");
			$data['pid'] = I("post.id");
			$data['start_time'] = I("post.start_time")/1000;
			$data['end_time'] = I("post.end_time")/1000;
			$data['status'] = 1;
			$data['cause'] = I("post.cause");
			$data['created'] = time();

			$info = $leave->add($data);

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//班主任 班长和组长已经进行审核的记录表
		// public function examined(){
		// 	$arr = array("id"=>$_SESSION['userid']);
		// }


		//待处理的事假申请   
		//组长  班长  班主任  所看到的是层级一级一级递上去的

		// 当前登录的用户是哪个班的  哪个小组的
		public function manage(){

			//查询登录用户的信息
			$users = D("users");
			$arr1 = array("id"=>$_SESSION['userid']);
			$info1 = $users->where($arr1)->find();

			//所有人员信息
			$usersinfo = $users->select();


			$class = D("class");
			//用户所在班
			$arr2 = array("id"=>$info1['class']);
			$info2 = $class->where($arr2)->find();

			$team = D("team");
			//用户所在组
			$arr3 = array("id"=>$info1['team']);
			$info3 = $team->where($arr3)->find();

			//查看请假事务
			$leave = D("leave");
			//用户的角色
			switch($info1['role_id']){
				case 1:					//管理员  校长  可以看，但不可以操作
					$tot = $leave->where("status>0 AND status<4")->count();
					break;
				case 2:					//管理员  校长  可以看，但不可以操作
					$tot = $leave->where("status>0 AND status<4")->count();
					break;
				case 5:					//班主任
					$tot = $leave->where("status=3")->count();
					break;
				case 8:					//班长
					$tot = $leave->where("status=2")->count();
					break;
				case 9:					//组长
					$tot = $leave->where("status=1")->count();
					break;

			}

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	
	    	//用户的角色
			switch($info1['role_id']){
				case 1:			//管理员  可以看，可以操作审核同意之后假条状态值直接转为4
					$list = $leave->where("status>0 AND status<4")->order("created desc")->limit($page->firstRow,$page->listRows)->select();
					break;
				case 2:			//校长  可以看，可以操作审核审核同意之后假条状态值直接转为4
					$list = $leave->where("status>0 AND status<4")->order("created desc")->limit($page->firstRow,$page->listRows)->select();
					break;

				case 5:					//班主任
					$list = $leave->where("status=3")->order("created desc")->limit($page->firstRow,$page->listRows)->select();
					break;
				case 8:					//班长
					$list = $leave->where("status=2")->order("created desc")->limit($page->firstRow,$page->listRows)->select();
					break;
				case 9:					//组长
					$list = $leave->where("status=1")->order("created desc")->limit($page->firstRow,$page->listRows)->select();
					break;

			}

			foreach($list as $key => $value){
				// 请假人姓名
				foreach($usersinfo as $k => $v){
					if($list[$key]['pid'] == $v['id']){
						$list[$key]['pid'] = $v['name'];
					}
				}
				// 假条状态
				$list[$key]['status'] = "审核中";

				// 请假天数
				$list[$key]['day'] = ceil(($list[$key]['end_time']-$list[$key]['start_time'])/(24*60*60));

				//开始时间
				$list[$key]['start_time'] = date("Y-m-d",$list[$key]['start_time']);
				//结束时间
				$list[$key]['end_time'] = date("Y-m-d",$list[$key]['end_time']);

			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Leave/untreated");
			
		}

		//进行审核处理

		//审核不通过
		public function notpass(){
			$arr = array("id"=>I("post.id"));
			//审核不通过  假条状态值设置为0
			$arrs = array("status"=>0);
			$leave = D("leave");
			$info = $leave->where($arr)->save($arrs);

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//批量审核不通过
		public function notpassAll(){
			$leave = D("leave");

			$arrs = $_POST['ids'];

			foreach($arrs as $k => $v){
				$arr = array("id"=>$v);
				$row = array("status"=>0);
				$info = $leave->where($arr)->save($row);
			}

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}

		}

		//审核通过
		public function pass(){
			$arr = array("id"=>I("post.id"));
			//审核通过  假条状态值要进行  +1
			// $arrs = array("status"=>0);
			$leave = D("leave");
			$userid = array("id" => $_SESSION['userid']);
			$users = D("users");
			$userinfo = $users->where($userid)->find();
			$list = $leave->where($arr)->find();
			if($userinfo['role_id']==1 || $userinfo['role_id']==2){
				$status = 4;
			}else{
				$status = $list['status']+1;
			}
			$arrss = array("status"=>$status);

			$info = $leave->where($arr)->save($arrss);

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//批量审核通过
		public function passAll(){
			$leave = D("leave");

			$arrs = $_POST['ids'];
			$a = array("id"=>$arrs[0]);

			//查看当前用户角色是管理员、校长  还是别的用户角色
			$userid = array("id" => $_SESSION['userid']);
			$users = D("users");
			$userinfo = $users->where($userid)->find();
			$list = $leave->where($arr)->find();

			if($userinfo['role_id']==1 || $userinfo['role_id']==2){
				$status = 4;
			}else{
				$status = $list['status']+1;
			}

			$list = $leave->where($a)->find();

			foreach($arrs as $k => $v){
				$arr = array("id"=>$v);
				$row = array("status"=>$status);
				$info = $leave->where($arr)->save($row);
			}

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//班级学员事假
		public function classManage(){
			//先把班级遍历出来
			$class = D("class");

			$users = D("users");
			$arr1['status'] = array("EGT",1);

			$classs = $class->where($arr1)->select();

			$search = "";
			//是否按照班级查找
			if(!empty($_GET['class'])){
				$tot = $class->where(array("id"=>$_GET['class']))->where($arr1)->count();
				$search="/id/{$_GET['class']}";
			}else{
				$tot = $class->where($arr1)->count();
			}
			// 分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);

	    	//是否按照班级查找
			if(!empty($_GET['class'])){
				$list = $class->where(array("id"=>$_GET['class']))->where($arr1)->limit($page->firstRow,$page->listRows)->select();
			}else{
				$list = $class->where($arr1)->limit($page->firstRow,$page->listRows)->select();
			}
			
			foreach($list as $key=>$value){
				//班主任
				$arr1 = array("id"=>$value['admin_id']);
				$info1 = $users->where($arr1)->find();
				$list[$key]['admin_id'] = $info1['name'];

				//讲师
				$arr2 = array("id"=>$value['teacher_id']);
				$info2 = $users->where($arr2)->find();
				$list[$key]['teacher_id'] = $info2['name'];

				//项目经理id
				$arr3 = array("id"=>$value['pm_id']);
				$info3 = $users->where($arr3)->find();
				$list[$key]['pm_id'] = $info3['name'];
				//班级状态
				switch($value['status']){
					case 0:
						$list[$key]['status'] = "即将开班";
						break;
					case 1:
						$list[$key]['status'] = "正在上课";
						break;
					case 2:
						$list[$key]['status'] = "已毕业";
						break;
				}
			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			// var_dump($list);
			// exit;
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->assign("search",$search);
			$this->assign("classs",$classs);
			$this->display("Leave/classManage");
		}

		//得到每个班级的假单
		public function classLeave(){
			//班级的id
			$arr1 = array("class"=>$_GET['classid']);
			//假单的状态要大于等于4
			$arrs['status'] = array("EGT",4);
			$leave = D("leave");

			$list = $leave->where($arr1)->where($arrs)->select();

			$users = D("users");
			foreach ($list as $key => $value) {
				// 学员姓名
				$arr2 = array("id"=>$value['pid']);
				$info2 = $users->where($arr2)->find();
				$list[$key]['pid'] = $info2['name'];
				// 请假天数
				$list[$key]['day'] = ceil(($list[$key]['end_time']-$list[$key]['start_time'])/(24*60*60));
				// 格式化时间
				$list[$key]['start_time'] = date("Y-m-d",$list[$key]['start_time']);
				$list[$key]['end_time'] = date("Y-m-d",$list[$key]['end_time']);
				
			}
			$class = D("class");
			$classname = rtrim($class->find($_GET['classid'])["name"], "班")."班请假数据";
			PHPExcel(4, $classname, $list);
		}
		
	}
?>